package com.demo.practice;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Trader raou1 = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raou1, 2012, 1000),
                new Transaction(raou1, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        List<Transaction> transactions2011 = transactions.parallelStream()
                                                         .filter(t -> t.getYear() == 2011)
                                                         .sorted(Comparator.comparing(t->t.getValue()))
                                                         .collect(Collectors.toList());
        System.out.println(transactions2011);

        List<String> cities = transactions.stream()
                                          .map(t -> t.getTrader().getCity())
                                          .distinct()
                                          .collect(Collectors.toList());
        System.out.println(cities);

        List<Trader> traders = transactions.stream()
                                           .map(Transaction::getTrader)
                                           .filter(tr -> tr.getCity().equals("Cambridge"))
                                           .sorted(Comparator.comparing(Trader::getName))
                                           .distinct()
                                           .collect(Collectors.toList());
        System.out.println(traders);

        List<String> traderNames = transactions.stream()
                                               .map(t -> t.getTrader().getName())
                                               .distinct()
                                               .sorted()
                                               .collect(Collectors.toList());
        System.out.println(traderNames);

        boolean workInMilan = transactions.stream()
                                           .map(t -> t.getTrader().getCity())
                                           .anyMatch(c -> c.equals("Milan"));
        System.out.println("work in Milan: " + workInMilan);

        int sum = transactions.stream()
                              .filter(t -> t.getTrader().getCity().equals("Cambridge"))
                              .map(Transaction::getValue)
                              .reduce(0, (a, b) -> a + b);
        System.out.println(sum);

        int maxValue = transactions.stream()
                                    .map(Transaction::getValue)
                                    .reduce(Integer::max)
                                    .get();
        System.out.println(maxValue);

        Optional<Transaction> smallestTransaction = transactions.stream()
                                                                .reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2);
        if(smallestTransaction.isPresent())
            System.out.println(smallestTransaction.get());
    }
}
